package cn.tedu.cs.baking.controller;

import cn.tedu.cs.baking.common.constant.AppConstant;
import cn.tedu.cs.baking.common.ex.BusinessException;
import cn.tedu.cs.baking.pojo.vo.PictureSimpleVO;
import cn.tedu.cs.baking.response.AppCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/file")
@Api(tags = "文件上传")
@Slf4j
public class UploadController {
    @Value("${file.save-path}")
    private String dirPath;

    @PostMapping("/upload")
    @ApiOperation("单文件上传")
    public PictureSimpleVO uploadFile(MultipartFile file) throws Exception {

        // 文件名非空校验
        if (file == null) {
            throw new BusinessException(AppCode.FILE_ERROR, "文件名不能为空");
        }
        // 大文件判定
        if (file.getSize() > AppConstant.MAX_SIZE) {
            throw new BusinessException(AppCode.FILE_ERROR, "文件过大，请使用大文件传输");
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            throw new BusinessException(AppCode.FILE_ERROR, "当前文件不是图片文件，无法转化图片存储");
        }

        PictureSimpleVO pictureVO = new PictureSimpleVO();

        //解析名称
        String picName = file.getOriginalFilename();
        if (StringUtils.isEmpty(picName)) {
            throw new BusinessException(AppCode.FILE_ERROR, "图片原名称为空");
        }
        int index = picName.lastIndexOf(".");
        if (index == -1) {
            throw new BusinessException(AppCode.FILE_ERROR, "图片后缀缺失");
        }
        String suffix = picName.substring(index).toLowerCase();
        if (!suffix.matches(".(png|jpg|jpeg|gif|img)")) {
            log.info("图片解析后缀为:" + suffix);
            throw new BusinessException(AppCode.FILE_ERROR, "图片后缀不规范,或非图片文件");
        }

        //图片重命名
        String picNewName = UUID.randomUUID().toString() + suffix;
        String datePath = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        File dirFile = new File(dirPath+datePath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        String sysPath = System.getProperty("user.dir");
        String path = sysPath.replaceAll("\\\\", "/");
        String filePath = path+dirPath.substring(1)+datePath+picNewName;
        file.transferTo(new File(filePath));

        pictureVO.setOldName(picName);
        pictureVO.setType(suffix);
        pictureVO.setSysName(picNewName);
        pictureVO.setUrl("/pic/"+datePath+picNewName);
        return pictureVO;
    }
}
